/**
 *Copyright [2010] [lcj(caijunlu@gmail.com)]
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License. 
 *You may obtain a copy of the License at 
 *             http://www.apache.org/licenses/LICENSE-2.0 
 *Unless required by applicable law or agreed to in writing, 
 *software distributed under the License is distributed on an "AS IS" BASIS, 
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 *either express or implied. See the License for the specific language governing permissions and limitations under the License
 */
package org.independent.servlet.session;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;

import org.independent.servlet.session.manager.SessionsManager;

/**
 * the Implementation of the HttpSession
 * 
 * @author lcj
 * 
 */
public class ServletSession implements HttpSession, Serializable {

	private static final long serialVersionUID = 1L;
	/**
	 * The maximum time interval, in seconds, between client requests before the
	 * servlet container may invalidate this session. A negative time indicates
	 * that the session should never time out.
	 */
	protected int maxInactiveInterval = -1;
	private String sessionId ;
	private long creationTime;
	private long lastAccessedTime = creationTime;
	// the last access the memcached time
	private long lastMemcachTime;

	// cached all of Attributes of the session
	private Map<String, Object> sessionMap = new HashMap<String, Object>();

	public Map<String, Object> getSessionMap() {
		return sessionMap;
	}

	public void setSessionMap(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public void setCreationTime(long creationTime) {
		this.creationTime = creationTime;
	}

	public void setLastAccessedTime(long lastAccessedTime) {
		this.lastAccessedTime = lastAccessedTime;
	}

	public long getLastMemcachTime() {
		return lastMemcachTime;
	}

	public void setLastMemcachTime(long lastMemcachTime) {
		this.lastMemcachTime = lastMemcachTime;
	}

	public Object getAttribute(String attributeName) {
		return sessionMap.get(attributeName);
	}

	public void put(String attributeName, Object value) {
		this.sessionMap.put(attributeName, value);
	}

	public void remove(String key) {
		this.sessionMap.remove(key);
	}

	public Object get(String key) {
		return this.sessionMap.get(key);
	}

	public Enumeration<?> getAttributeNames() {
		// TODO for test
		return ((ServletContext) sessionMap).getAttributeNames();
	}

	public long getCreationTime() {
		return this.creationTime;
	}

	public String getId() {
		return this.sessionId;
	}

	public long getLastAccessedTime() {
		return this.lastAccessedTime;
	}

	public int getMaxInactiveInterval() {
		return 0;
		// TODO
	}

	public ServletContext getServletContext() {
		// TODO Auto-generated method stub
		return null;
	}

	public HttpSessionContext getSessionContext() {
		// TODO Auto-generated method stub
		return null;
	}

	public Object getValue(String attributeName) {
		return sessionMap.get(attributeName);
	}

	public String[] getValueNames() {
		return (String[]) sessionMap.keySet().toArray();
	}

	public void invalidate() {
		SessionsManager.getInstance().cleanSession();
	}

	public HttpSession getSession() {
		return this;
	}

	public void putValue(String attributeName, Object value) {
		setAttribute(attributeName, value);
	}

	public void removeAttribute(String attributeName) {
		sessionMap.remove(attributeName);
		SessionsManager.getInstance().storeSession(this);
	}

	public void removeValue(String attributeName) {
		sessionMap.remove(attributeName);
		SessionsManager.getInstance().storeSession(this);
	}

	public void setAttribute(String name, Object value) {
		this.put(name, value);
	}


	public void setMaxInactiveInterval(int arg0) {
		// TODO

	}

	public boolean isNew() {
		return false;
	}

}
